<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace buwang\service;

use think\facade\Cookie;
use think\facade\Session;
use app\common\model\MemberMiniapp;
use app\common\model\User;
use encrypter\Encrypter;
use filter\Inspect;
use app\manage\model\Admin;
use app\manage\model\Member;

class UserService
{

    //const session_scope = 'buwangyun#com/user/scope';        //SESSION作用域
    const session_name = 'current_user';        //SESSION值
    const key = 'buwangyun#com@buwang';  //加密秘钥

    /**
     * ##########################################
     * 判断管理员是否登录
     * @access public
     * @return boolean
     */
    public static function isLogin()
    {
        return Session::has(self::session_name) ? true : false;
    }

    /**
     * 获取用户详细信息
     * @return boolean/array
     */
    public static function getUser()
    {
        if (self::isLogin()) {
            $user = User::find(self::getLogin('id'));
            if (empty($user)) return false;
            if ($user['is_lock'] == 0) {
                return $user;
            }
        }
        return false;
    }

    /**
     * 获取登录Session中保存的数据
     * @param string $key数组键名 (id,username,ucode);
     * @return void
     */
    public static function getLogin(string $key)
    {
        if (self::isLogin()) {
            $info = Session::get(self::session_name);
            $login_info = json_decode(Encrypter::bwDecode($info, self::key), true);
            return empty($key) ? $login_info : $login_info[$key];
        }
        return false;
    }

    /**
     * 设置登录Session
     * @param $param
     * @return void
     */
    public static function setLogin($param)
    {
        if (isset($param['id'])) {
            $data = array_merge([
                'id' => $param['id'],
                'login_time' => time(),
            ], $param);
        } else {
            $data = $param;
        }
        $key = Encrypter::bwEncode(json_encode($data), self::key);

        //Cookie::set('token', $param['token']);

        return Session::set(self::session_name, $key);
    }

    /**
     * 退出Session
     * @access public
     */
    public static function setlogout()
    {
        return Session::delete(self::session_name);
    }


    /**
     * ##########################################
     * 设置邀请码
     * @access public
     */
    public static function setUcode()
    {
        $ucode = input('get.ucode', '', 'htmlspecialchars');

        if (isset($ucode)) {
            Cookie::set('ucode', Inspect::filter_escape($ucode), 3600 * 24);
            return $ucode;
        }
    }

    /**
     * 获取邀请码
     * @access public
     */
    public static function getUcode()
    {
        if (Cookie::has('ucode')) {
            return Cookie::get('ucode');
        }
        return false;
    }

    /**
     * 删除邀请码
     * @access public
     */
    public static function detUcode()
    {
        return Cookie::delete('ucode');
    }

    /**
     * 判断创始人,并读取后绑定的创始人的信息
     * @access public
     */
    public static function isFounder($miniapp_id)
    {
        $miniapp = MemberMiniapp::where(['id' => $miniapp_id])->field('uid')->find();
        if (empty($miniapp->user)) {
            $data['user_id'] = 0;
        } else {
            $data['user_id'] = $miniapp->user->id;
            $data['invite_code'] = $miniapp->user->invite_code;
            $data['mobile'] = $miniapp->user->phone_uid;

            $data['avatar'] = $miniapp->user->avatar;
            $data['nickname'] = $miniapp->user->nickname;
            $data['miniapp_uid'] = $miniapp->user->miniapp_uid;
            $data['official_uid'] = $miniapp->user->official_uid;
            $data['login_time'] = date('Y-m-d', $miniapp->user->login_time);
        }
        return (object)$data;
    }

    /**判断登录密码是否正确
     * @param $username
     * @param $password
     */
    public static function validateLogin($username, $password)
    {

        return ['rs' => Admin::validateLogin($username, $password), 'error' => Admin::getError()];
    }

    public static function loginAdmin($admin, $trans = false)
    {
        return ['rs' => Admin::loginAdmin($admin, $trans), 'error' => Admin::getError()];

    }

    public static function validateMember($username, $password)
    {

        return ['rs' => Member::validateMember($username, $password), 'error' => Member::getError()];
    }

    public static function loginMember($admin, $trans = false)
    {
        return ['rs' => Member::loginMember($admin, $trans), 'error' => Member::getError()];

    }

    public static function validateUser($username, $password)
    {
        return ['rs' => User::validateUser($username, $password), 'error' => User::getError()];

    }

    public static function loginUser($user, $scopes, $trans = false)
    {
        return ['rs' => User::loginUser($user, $scopes, $trans), 'error' => User::getError()];

    }
}